//
//  WoundDesCellEditLeft.m
//  WSJWoundDesVC
//
//  Created by wsj2015 on 16/3/30.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "WoundDesCellEditLeft.h"

@implementation WoundDesCellEditLeft

- (void)awakeFromNib {
    // Initialization code
    self.leftTf.enabled = NO;
    [self.selectBtn setImage:[UIImage imageNamed:@"evaluation_unselected"] forState:UIControlStateNormal];
    [self.selectBtn setImage:[UIImage imageNamed:@"evaluation_selected"] forState:UIControlStateSelected];
    self.leftTf.delegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (IBAction)selectAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (self.model.readonly ==NO) {
        if (sender.selected ) {
            self.contentLabel.textColor = [UIColor blackColor];
            self.leftTf.enabled =YES;
        }else{
            self.contentLabel.textColor = [UIColor grayColor];
            self.leftTf.enabled = NO;
            self.leftTf.text = nil;
        }
    }
    
    if (self.btnBlock) {
        self.btnBlock(sender.selected);
    }
}
-(void)setModel:(WoundContentModel *)model{
    [super setModel:model];
     self.contentLabel.text = [model.content stringByReplacingOccurrencesOfString:@"?" withString:@""];
    self.selectBtn.selected = model.isSelect;
    if (!model.isSelect &&model.readonly ==NO) {
        self.contentLabel.textColor = [UIColor grayColor];
    }else{
        self.contentLabel.textColor = [UIColor blackColor];
    }
    
    
    if (!model.isSelect) {
        self.leftTf.text =nil;
    }else{
        self.leftTf.text =model.editStrO;
    }
    if ([self.model.inputType isEqualToString:@"num"]) {
        self.leftTf.keyboardType = UIKeyboardTypeDecimalPad;
    }else if([self.model.inputType isEqualToString:@"txt"]){
        self.leftTf.keyboardType = UIKeyboardTypeDefault;
    }else{
        self.leftTf.keyboardType = UIKeyboardTypeDefault;
        
    };
    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([self.model.inputType isEqualToString:@"num"]) {
        NSMutableString *text = [NSMutableString stringWithString:textField.text];
        [text replaceCharactersInRange:range withString:string];
        
        if (text.length == 0) {
            return YES;
        }
        if ([string isEqualToString:@"."]) {
            return YES;
        }
        NSScanner* scan = [NSScanner scannerWithString:text];
        float val;
        
        return [scan scanFloat:&val] && [scan isAtEnd];
    }
    
    
    return YES;

}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    self.model.editStrO = textField.text;
    return YES;
}
@end
